/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  KeyPairServiceImpl.java   
 * @Package com.axin.service.impl   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年3月18日 下午9:57:16   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.axin.dao.HbKeyPairMapper;
import com.axin.entity.HbKeyPair;
import com.axin.entity.example.HbKeyPairExample;
import com.axin.service.KeyPairService;

/**   
 * @Description: TODO 
 * @ClassName:  KeyPairServiceImpl
 * @author:  Axin 
 * @date:   2019年3月18日 下午9:57:16   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
@Service
public class KeyPairServiceImpl implements KeyPairService {
	
	@Autowired
	private HbKeyPairMapper keyPairMapper;


	@Override
	public HbKeyPair getKeyPairByUserId(Long userId) {
		HbKeyPairExample example = new HbKeyPairExample();
		example.createCriteria().andUserIdEqualTo(userId);
		List<HbKeyPair> list = this.keyPairMapper.selectByExample(example);
		if(CollectionUtils.isNotEmpty(list)){
			return list.get(0);
		}
		return null;
	}


	/* (non-Javadoc)
	 * @see com.axin.service.KeyPairService#saveKeyPair(com.axin.entity.HbKeyPair)
	 */
	@Override
	public Long saveKeyPair(HbKeyPair keyPair) {
		Long id = keyPair.getId();
		keyPair.setUpdateTime(new Date());
		if(id == null){
			keyPair.setCreateTime(new Date());
			this.keyPairMapper.insert(keyPair);
			id = keyPair.getId();
		}else{
			HbKeyPair Pair = this.keyPairMapper.selectByPrimaryKey(id);
			Pair.setAccessKey(keyPair.getAccessKey());
			Pair.setSecretKey(keyPair.getSecretKey());
			Pair.setAccessPassword(keyPair.getAccessPassword());
			this.keyPairMapper.updateByPrimaryKey(Pair);
		}
		return id;
		
	}


	/* (non-Javadoc)
	 * @see com.axin.service.KeyPairService#getKeyPairById(java.lang.Long)
	 */
	@Override
	public HbKeyPair getKeyPairById(Long id) {
		return this.keyPairMapper.selectByPrimaryKey(id);
	}
	
	
	
	
	

}
